<?php
/**
 * 手机端 - 设计师团队
 * User: eecjimmy
 * Date: 2015-06-17
 */

namespace Wap\Controller;

use Think\Page;

class DesignerController extends WapController {
    /**
     * @var \Think\Model
     */
    private $m;

    function __construct() {
        parent::__construct();
        $this->m = M('designer');
    }

    // 设计师团队列表
    function index() {
        $limit = 5; // 每页条数
        I('tid')||$_GET['tid'] = 1;
        $where = array(
            'faxiu_designer.is_recommend' => 1, // 推荐设计师
            'faxiu_designer.tid'=>I('tid',1), // 默认分类第一个
        );
        $order = array(
            'faxiu_designer.sort asc',
            'faxiu_designer.id desc'
        );
        $field = array(
//            'faxiu_picture.path pic_path', // 设计师头像(路径)
            'faxiu_designer.id', // 设计师id
            'faxiu_designer.pic', // 设计师图片
            'faxiu_designer.dname', // 设计师姓名
            'faxiu_designer.job', // 设计师职位
        );
        $join = array(
//            'LEFT JOIN faxiu_picture ON faxiu_picture.id=faxiu_designer.pic',
        );

        //设计师分类
        $type = M("designer_type")->order("sort")->select();
        $this->assign("type",$type);

        // 分页
        $count = $this->m->where($where)->count();
        if(isset($_REQUEST['fl'])) unset($_REQUEST['fl']);
        isset($_REQUEST['tid']) || $_REQUEST['tid']=1;
        $page = new Page($count, $limit, $_REQUEST);
        if ($count > $limit) {
            $page->setConfig('theme', '%DOWN_PAGE%');
            $page->setConfig('next', '加载更多...');
        }
        $pageNav = $page->show();
        $offset = $page->firstRow;

        $list = $this->m->where($where)->join($join)->order($order)->limit($offset, $limit)->field($field)->select();
        foreach($list as &$item){
            $pics = explode(',',$item['pic']);
            $item['pic_path'] = get_cover($pics[0],'path');
        }
        $this->assign('list', $list);
        if (!IS_AJAX) {
            $this->assign('pageNav', $pageNav);
            $this->display();
        } elseif (IS_AJAX && I('fl')) {
            $this->assign('pageNav', $pageNav);
            $this->ajaxReturn(array('html' => $this->fetch('body'), 'status' => true));
        } else {
            $this->ajaxReturn(array('html' => $this->fetch('list'), 'pageNav' => $pageNav));
        }
    }

    // 设计师详情
    function detail($id) {
        $where = array(
            'faxiu_designer.id' => (int)$id,
        );
        $field = array(
//            'faxiu_picture.path pic_path',
            'faxiu_designer.*',
           // 'faxiu_store.location store_location', // 设计师所在分店坐标
            'faxiu_store.address store_address', // 设计师所在分店地址
            'faxiu_store.lon lon', // 设计师所在分店维度
            'faxiu_store.lat lat', // 设计师所在分店经度
            'faxiu_store.sname store_name', // 设计师所在分店地址
        );
        $join = array(
//            'LEFT JOIN faxiu_picture ON faxiu_picture.id=faxiu_designer.pic',
            'LEFT JOIN faxiu_store on faxiu_store.id=faxiu_designer.sid',
        );
        $designer = $this->m->where($where)->join($join)->field($field)->find();
        $designer["store_location"] = $designer["lon"].",".$designer["lat"];
        $pics = explode(',',$designer['pic']);
        foreach($pics as $pic_id){
            $designer['pic_path'][] = get_cover($pic_id,'path');
        }
        $designer === null && E('设计师不存在');
        $designer['is_collect'] = $this->_is_collected($designer['id'], 3);        // 判断该当前微信号是否收藏该设计师
        $designer['new_works'] = $this->_get_works_by_designer($designer['id'], 3); // 该设计师的三个最新作品
        $designer['store_location'] = implode(',', array_reverse(explode(',', $designer['store_location'])));// 经纬度互换位置
        $designer['collect_total'] = M('collect')->where(array('tid' => $designer['id'], 'type' => 3))->count(); // 收藏量
        $this->assign('designer', $designer);
        $this->display();
    }

    // 收藏设计师
    function collect($tid,$type) {
        // 判断是否收藏过该设计师
        if ($this->_is_collected($tid, $type)) {
            $where['mid'] = $this->member['id'];
            $where['tid'] = $tid;
            $where['type'] =$type;
            if (M('collect')->delete($where)) {
                //将商品收藏量减一1
                if ($type == 2) {
                    $product["id"] = array("eq", $tid);
                    M("product")->where($product)->setDec('collect', 1);
                }
                $this->ajaxReturn(array('status' => true, 'action' => 'remove', 'info' => '取消收藏成功'));
            } else {
                $this->ajaxReturn(array('status' => false, 'info' => '取消收藏失败'));
            }
        } else {
            $data['type'] = $type;
            $data['mid'] = $this->member['id'];
            $data['tid'] = $tid;
            $data['add_time'] = NOW_TIME;
            if (M('collect')->add($data)) {
                //将商品收藏量加1
                if ($type == 2) {
                    $product["id"] = array("eq", $tid);
                    M("product")->where($product)->setInc('collect', 1);
                }


                $this->ajaxReturn(array('status' => true, 'action' => 'add', 'info' => '收藏成功'));
            } else {
                $this->ajaxReturn(array('status' => false, 'info' => '收藏失败'));
            }
        }
    }

    /**
     * 通过设计师获取最新作品数量
     * @param int $designer_id 设计师id
     * @param int $limit       获取数量 - 默认获取全部
     * @return array
     */
    function _get_works_by_designer($designer_id, $limit = 0,$id=0) {
        $sort = array(
            'collect_nums desc'
        );
        $field = array(
            'count(faxiu_collect.id) as collect_nums',
            'faxiu_works.id',
            'faxiu_works.wname',
            'faxiu_picture.path as pic_path' //作品图片
        );
//        $where = array(
//            'faxiu_works.did' => $designer_id,
//        );
        $where['faxiu_works.did'] = array("eq",$designer_id);
        if($id!=0){
            $where['faxiu_works.id'] = array("neq",$id);
        }
        $limit && M('works')->limit($limit);
        $join = array(
            'LEFT JOIN faxiu_picture ON faxiu_picture.id=faxiu_works.pic',
            'LEFT JOIN faxiu_collect on faxiu_collect.tid=faxiu_works.id'
        );
        $group = 'faxiu_works.id';
        return M('works')->group($group)->where($where)->order($sort)->join($join)->field($field)->select();
    }
}
